<?php

namespace App\Http\Controllers\User;

use App\Model\User;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use BindService;

class BindController extends Controller
{

    public function show(Request $request)
    {
        if(\Auth::guard('wechat')->user()->number !== null){
            return redirect()->to(config('wechat.url.bbs'));
        }

        try{
            $prepare = BindService::prepare();
        }catch(\Exception $e){
            return redirect('error/back');
        }

        return view('app.user.bind', $prepare);
    }

    public function code($session, $nonce, $cookie=null)
    {
        $code = BindService::getCode($session, $cookie);
        return response($code)->header('Content-Type', 'image/gif');
    }

    public function bind(Request $request)
    {
        $this->validate($request, [
            'txtUserName' => 'required',
            'TextBox2' => 'required'
        ]);

        $result = BindService::bind($request);

        return response()->json([
            'msg' => $result === true ? 'ok' : $result,
            'target' => session('targetUrl') ? : config('wechat.url.bbs'),
        ]);
    }
}
